﻿package j.sus2maj7.util
{
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileStream;
	import flash.filesystem.FileMode;
	import flash.net.FileFilter;
	import flash.utils.ByteArray;
	
	public class FileUtil
	{
		public static function read(f:File):ByteArray
		{
			var b:ByteArray = new ByteArray();
			var fs:FileStream = new FileStream();
			
			fs.open(f, FileMode.READ);
			fs.readBytes(b, 0, 0);
			
			return b;
		}
		public static function browse(filter:FileFilter, title:String, callback:Function):void
		{
			var f:File = new File();
			
			try
			{
				f.browseForOpen(title, [filter]);
				f.addEventListener(Event.SELECT, callback, false, 0, true);
			}
			catch (error:Error)
			{
				trace("Failed Opening File:", error.message);
			}
			
		}
		
		public static function doesExist(f:File):Boolean
		{
			if (f == null || !f.exists)
			{
				return false;
			}
			else return true;
		}
	}
	
}